﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 销毁档案操作
    /// </summary>
    public class V_ArchiveBgqxMjDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 可以销毁的档案信息
        /// </summary>
        /// <returns></returns>
        public static List<V_ArchiveBgqxMj> Get_Archive(out int count,string  dep,int YEAR, int arid=-1,int? del = null,int start =-1, int row=-1)
        {
            count = 0;
            try
            {
                int year = DateTime.Now.Year;
                if (YEAR > -1)
                    year = YEAR;
                conn = new SGArchiveSystem7xEntities();
                var query = conn.V_ArchiveBgqxMj.Select(c => c).Where(c => c.desyear < year && c.bgqxvalue != 0);
                if (arid > -1)
                    query = query.Where(c => c.ARID == arid);
                if (dep !=null&&dep!="")
                    query = query.Where(c => c.dep_name.Contains(dep));
                query = query.Where(c => c.DEL == del);
                if(start > -1 && row > -1)
                {
                    //var list = query.OrderBy(c=>c.ND).ThenBy(c => c.ARID).OrderBy(c=>c.ID).ToList();
                    count = query.Count();
                    return query.OrderBy(c => c.ND).ThenBy(c => c.ARID).OrderBy(c => c.ID).Skip(start).Take(row).ToList();;
                }
                else
                {
                    count = query.Count();
                    var list = query.OrderBy(c => c.ND).ThenBy(c => c.ARID).OrderBy(c => c.ID).ToList();
                    return list;
                }
            }
            catch { return null; }
        }
        public static List<T_DestroyArchive> Get_DestoryArchive(out int count, int status,string name, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_DestroyArchive.Select(c => c);
                if (status > -1)
                    query = query.Where(c => c.DES_STATUS == status);
                if (name != null && name != "")
                    query = query.Where(c => c.DES_NAME.Contains(name));
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.DES_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch { return null; }
        }
        public static int Add_Destory(T_DestroyArchive en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_DestroyArchive.Select(c => c).Where(c => c.DES_NAME == en.DES_NAME).Count() > 0)
                    return 0;
                else
                {
                    conn.T_DestroyArchive.Add(en);
                    conn.SaveChanges();
                    return en.DES_ID;
                }
            }
            catch (Exception ex) { return -1; }
        }
        public static int Modify_Archive(string dh,int del)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveAj.Select(c=>c).Where(c=>c.DH==dh);
                foreach (var nn in query)
                {
                    nn.DEL = del;
                }
                conn.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public static int QRXH_Destory(T_DestroyArchive en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_DestroyArchive.Select(c => c).Where(c => c.DES_ID == en.DES_ID);
                foreach (var nn in query)
                {
                    nn.DES_STATUS = 2;//确认过销毁
                    nn.DES_V01 = en.DES_V01;//上级领导
                    nn.DES_V02 = en.DES_V02;//附件
                }
                conn.SaveChanges();
                int count = 0;
                var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count, null, -1, -1, en.DES_ID, -1, -1);
                foreach (var nn in list) {
                    Modify_Archive(nn.ID);
                }
                return 1;
            }
            catch (Exception ex) { return -1; }
        }
        public static int Modify_Archive(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveAj.Select(c => c).Where(c => c.ID == id);
                foreach (var nn in query)
                {
                    nn.KFZT = 4;
                }
                conn.SaveChanges();
                return 1;
            }
            catch (Exception ex) { return -1; }
        }
        //public static List<T_ArchiveAj> Get_QCArchive(out int count, int del, int start = -1, int row = -1)
        //{
        //    count = 0;
        //    try
        //    {
        //        conn = new SGArchiveSystem7xEntities();
        //        var query = conn.T_ArchiveAj.Select(c => c);
        //        if (del > -1)
        //            query = query.Where(c => c.DEL == del);
        //        if (start > -1 && row > -1)
        //        {
        //            var list = query.OrderBy(c => c.ND).ThenBy(c => c.ARID).OrderBy(c => c.ID).ToList();
        //            count = list.Count;
        //            return list;
        //        }
        //        else
        //        {
        //            count = query.Count();
        //            var list = query.OrderBy(c => c.ND).ThenBy(c => c.ARID).OrderBy(c => c.ID).Skip(start).Take(row).ToList();
        //            return list;
        //        }
        //    }
        //    catch { return null; }
        //}
    }
}
